#!/bin/sh -e
#
# runuo init script 
#

# Check the package is still installed
[ -x /usr/games/runuos/runuo.exe ] || exit 0
[ -x /usr/games/runuos/runuos ] || exit 0

# Get LSB functions
. /lib/lsb/init-functions
. /etc/default/rcS

runuo_stop() {
	PID=`pidof mono /usr/games/runuos/runuo.exe`
	echo "killing ${PID} ..."
	kill $PID > /dev/null 2>&1
	if [ `pidof mono /usr/games/runuos/runuo.exe` ]; then
		echo "... failed!"
		return 1
	fi
	return 0
}

case "$1" in
    start)
	[ -d /var/run/runuos ] || mkdir /var/run/runuos
	if [ `pidof mono /usr/games/runuos/runuo.exe` ]; then
		log_begin_msg "RunUO is already running ..."
		log_end_msg 1
	else
		log_begin_msg "Starting RunUO ..."
		/usr/games/runuos/runuos &
		log_end_msg $?
	fi
	;;
    stop)
	log_begin_msg "Stopping RunUO..."
	runuo_stop
	log_end_msg $?
	;;
    restart|force-reload)
	log_begin_msg "Stopping RunUO..."
	if runuo_stop; then
		log_end_msg 0
	else
		log_end_msg 1
	fi

	[ -d /var/run/runuos ] || mkdir /var/run/runuos
	if [ `pidof mono /usr/games/runuos/runuo.exe` ]; then
		log_begin_msg "RunUO is already running ..."
		log_end_msg 1
	else
		log_begin_msg "Starting RunUO ..."
		/usr/games/runuos/runuos &
		log_end_msg $?
	fi
	;;
    *)
	echo "Usage: /etc/init.d/runuos {start|stop|restart|force-reload}"
	exit 1
	;;
esac

exit 0
